from pymilvus import connections, utility


def reset_collection(collection_name: str = "pdf_documents"):
    """删除并重建Milvus集合"""
    try:
        # 连接到Milvus服务器
        connections.connect(
            alias="default",
            host="192.168.1.20",
            port="19530"
        )

        # 检查集合是否存在
        if utility.has_collection(collection_name):
            print(f"正在删除集合: {collection_name}")
            utility.drop_collection(collection_name)
            print("集合删除成功！")
        else:
            print(f"集合 {collection_name} 不存在")

        print("现在可以重新运行主程序来创建新的集合")

    except Exception as e:
        print(f"发生错误: {str(e)}")
    finally:
        # 断开连接
        connections.disconnect("default")


if __name__ == "__main__":
    reset_collection()